{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.2-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38232bitd1a45618de2048beaac3236600337bba",
   "display_name": "Python 3.8.2 32-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "the corresponding Fahrenheit data -17.2\nNo value error\n"
    }
   ],
   "source": [
    "# Functions: from Fahrenheit to Celsius\n",
    "# Author:   Chris Wang\n",
    "# eqution:   f = c*1.8 +32\n",
    "def cel2fah(src):\n",
    "    if src < -273:\n",
    "        raise ValueError('Value not exists')\n",
    "\n",
    "    res = 5 * (src - 32)/9.0\n",
    "\n",
    "    return res\n",
    "\n",
    "try:\n",
    "    cel = float (input(\"Please input Celsius value\"))\n",
    "\n",
    "    fah = cel2fah(cel)\n",
    "\n",
    "    print(\"the corresponding Fahrenheit data %0.1f\" % (fah))\n",
    "\n",
    "except ValueError('Value not exists'):\n",
    "    print(\"Celsius Value not exists\")\n",
    "else:\n",
    "    print(\"No value error\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function: judge a interger is prime or not\n",
    "# Author:   Chris Wang\n",
    "min_prime = 2 # minimum prime is 2\n",
    "\n",
    "def is_prime(temp):\n",
    "    #judge input string is decimal only, -  , binary, roma are not allowed \n",
    "    if(temp.isdecimal() == False):  \n",
    "        return False\n",
    "\n",
    "    temp = int(temp)\n",
    "\n",
    "    # if input is 1,0,or negetive, indicates no prime   \n",
    "    if(temp < min_prime): \n",
    "        return False\n",
    "\n",
    "    max_bound = temp/2 + 1\n",
    "    max_bound = int(max_bound)\n",
    "    for i in range(min_prime, max_bound, 1):\n",
    "        if(temp % i == 0):\n",
    "            return False\n",
    "    else: \n",
    "        return True\n",
    "\n",
    "assert(isPrime('data') == False)\n",
    "assert(isPrime('-3.1') == False)\n",
    "assert(isPrime('3') == True)\n",
    "assert(isPrime('67') == True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function: calculate sum of 0-9 in a string\n",
    "# Author:   Chris Wang\n",
    "def sum_digit(src):\n",
    "    if src.count('.') > 1:\n",
    "        raise ValueError(\"str error\")\n",
    "    sum = 0\n",
    "    for i in range( len(src) ):\n",
    "        if(src[i] >= '0' and src[i] <= '9'):\n",
    "            sum = sum + int( src[i] )\n",
    "    return sum\n",
    "\n",
    "assert(sum_digit(\"124.56\") == 18)\n",
    "assert(sum_digit(\"156\") == 12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "origin list:[4, 89, 97, 40, 74, 30, 57, 72, 85, 19, 69, 13, 30, 91, 70, 62, 54, 72, 27, 99, 64, 92, 86, 73, 91, 13, 15, 93, 15, 95]\nSorting out:{4, 13, 15, 19, 27, 30, 40, 54, 57, 62, 64, 69, 70, 72, 73, 74, 85, 86, 89, 91, 92, 93, 95, 97, 99}\n"
    }
   ],
   "source": [
    "# Function: delete repeted char\n",
    "# Author : Chris Wang\n",
    "import random\n",
    "\n",
    "# generate a random list\n",
    "def random_list(start,stop,length):\n",
    "    # bound dection \n",
    "    if (length >= 0):\n",
    "        length=int(length)\n",
    "\n",
    "    start, stop = (int(start), int(stop)) \n",
    "    # bound dection \n",
    "    if (start > stop):\n",
    "        start, stop = (int(stop), int(start))\n",
    "    \n",
    "    random_list = []\n",
    "\n",
    "    for i in range(length):\n",
    "        # add item to the end of the list \n",
    "        random_list.append(random.randint(start, stop))  \n",
    "\n",
    "    return random_list\n",
    "\n",
    "num_start = 0\n",
    "num_end = 100\n",
    "num_cnt = 30 # generate 30 numbers\n",
    "lst = random_list(num_start,num_end,num_cnt)\n",
    "print(\"origin list:\" + str(lst))\n",
    "\n",
    "lst_reg  = []\n",
    "\n",
    "# for i in range( len(lst) ):\n",
    "#     if lst[i] not in lst_reg:\n",
    "#         lst_reg.append( lst[i] ) # no return value function\n",
    "# print(\"Sorting out:\" + str(lst_reg))\n",
    "\n",
    "lst_reg = set(lst)\n",
    "print(\"Sorting out:\" + str(lst_reg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fun(temp):\n",
    "    yield temp*2\n",
    "    print(\"*\")\n",
    "\n",
    "for i in fun(5):\n",
    "    print(\"x\"*10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ZFile(object):\n",
    "    def __init__(self, filename, mode='r', basedir=''):\n",
    "        self.filename = filename\n",
    "        self.mode = mode\n",
    "        if self.mode in ('w', 'a'):\n",
    "            self.zfile = zipfile.ZipFile(\n",
    "                filename, self.mode, compression=zipfile.ZIP_DEFLATED)\n",
    "        else:\n",
    "            self.zfile = zipfile.ZipFile(filename, self.mode)\n",
    "        self.basedir = basedir\n",
    "        if not self.basedir:\n",
    "            self.basedir = os.path.dirname(filename)\n",
    "\n",
    "    def addfile(self, path, arcname=None):\n",
    "        path = path.replace('//', '/')\n",
    "        if not arcname:\n",
    "            if path.startswith(self.basedir):\n",
    "                arcname = path[len(self.basedir):]\n",
    "            else:\n",
    "                arcname = ''\n",
    "        self.zfile.write(path, arcname)\n",
    "\n",
    "    def addfiles(self, paths):\n",
    "        for path in paths:\n",
    "            if isinstance(path, tuple):\n",
    "                self.addfile(*path)\n",
    "            else:\n",
    "                self.addfile(path)\n",
    "\n",
    "    def close(self):\n",
    "        self.zfile.close()\n",
    "\n",
    "    def extract_to(self, path):\n",
    "        for p in self.zfile.namelist():\n",
    "            self.extract(p, path)\n",
    "\n",
    "    def extract(self, filename, path):\n",
    "        if not filename.endswith('/'):\n",
    "            f = os.path.join(path, filename)\n",
    "            dir = os.path.dirname(f)\n",
    "            if not os.path.exists(dir):\n",
    "                os.makedirs(dir)\n",
    "            self.zfile(f, 'wb').write(self.zfile.read(filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'_io.TextIOWrapper' object has no attribute 'softspace'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-f43105d6b43d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'.\\data\\people.txt'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msoftspace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[0mread_txt\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mread_txt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mrow\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'\\n'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# delete '\\n'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: '_io.TextIOWrapper' object has no attribute 'softspace'"
     ]
    }
   ],
   "source": [
    "with open('.\\data\\people.txt','r') as fp:\n",
    "    print(fp.name)\n",
    "    read_txt = fp.readlines()\n",
    "    for row in read_txt:\n",
    "        row = row.strip('\\n') # delete '\\n'\n",
    "        if row == 1:\n",
    "            continue\n",
    "        \n",
    "    print(read_txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}